#include "frame.h"

using namespace std;

Frame::Frame()
{
    init( 0, 0, -1.0 );
}

Frame::Frame( int w, int h, double t )
{
    init( w, h, t );
}

Frame::~Frame()
{
    delete [] pixels;
}

void Frame::init( int w, int h, double t )
{
    _w = w;
    _h = h;
    _t = t;
    if( w > 0 && h > 0 )
        pixels = new Color[ w * h ];
    else
        pixels = NULL;
}

void Frame::setPixel( int i, int j, const Color& color )
{
    if( _w > 0 && _h > 0 )
        pixels[ i * _w + j ] = color;
}

void Frame::save( const string& fileName )
{
    if( _w == 0 || _h == 0 )
        return;
    FILE* outFile;
    outFile = fopen( fileName.c_str(), "w" );
    fprintf( outFile, "P3\n" );
    fprintf( outFile, "%i %i\n", _w, _h );
    fprintf( outFile, "255\n\n" );
    int idx;
#pragma mta serial
    for( int i=0; i<_w*_h; i++ )
        fprintf( outFile, "%i %i %i ", pixels[i].R(), pixels[i].G(), pixels[i].B() );
    fprintf( outFile, "\n" );
    fclose( outFile );
}

int Frame::w() const
{
    return _w;
}

int Frame::h() const
{
    return _h;
}

double Frame::t() const
{
    return _t;
}
